package cn.his.medical.pacshism;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;

import cn.his.medical.pacshism.adapter.ViewPagerAdapter;
import cn.his.medical.pacshism.bean.ViewPagerBean;
import cn.his.medical.pacshism.view.AutoViewPager;
import cn.his.medical.pacshism.view.BannerView;

public class MainActivity extends AppCompatActivity {
    private BannerView bannerView;
    private TextView title;
    private MyAdapter adapter1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //无title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,
                WindowManager.LayoutParams. FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        AutoViewPager vp = findViewById(R.id.vp);
        final ViewPagerAdapter adapter = new ViewPagerAdapter(MainActivity.this,initdata());;
        vp.setAdapter(adapter);
        bannerView = findViewById(R.id.view_pager);
        bannerView.setBannerDuring(3000);
        adapter1 = new MyAdapter(this, initdata());
        bannerView.setAdapter(adapter1);
        title = findViewById(R.id.title);
        bannerView.setOnBannerChangeListener(new BannerView.OnBannerChangeListener() {
            @Override
            public void onBannerChanged(int pos) {
                title.setText(adapter1 == null ? "" : adapter1.getItem(pos).getT_user_name());
            }
        });
    }

    public List<ViewPagerBean> initdata(){
        List<ViewPagerBean> list = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            ViewPagerBean bean = new ViewPagerBean();
            bean.setT_sub_head("杀鸡风华绝代撒回复大法好"+i);
            bean.setT_user_name("日龙包"+i);
            bean.setT_intro(getResources().getString(R.string.biography)+i);
            bean.setT_show_time("20188年44月232日"+i);
            bean.setT_show_address("月球上的山顶顶上的狗狗头"+i);
            bean.setT_show_phone("678967890"+i);
            list.add(bean);
        }
        return list;
    }
    private Handler mhandr = new Handler(Looper.getMainLooper());
    private void Stop(){

    }

    public void start(){
        mhandr.postDelayed(task ,2000);
        mhandr.removeCallbacks(task);
    }
    private Runnable task =  new Runnable() {
        @Override
        public void run() {

        }
    };

    private class MyAdapter extends BaseAdapter {

        public Context context;
        private List<ViewPagerBean>  list ;
        public MyAdapter(Context context,List<ViewPagerBean>  list) {
            this.context = context;
            this.list = list;
        }

        @Override
        public int getCount() {
            return list == null ? 0 : list.size();
        }

        @Override
        public ViewPagerBean getItem(int i) {
            return list == null ? null : list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int position, View view, ViewGroup viewGroup) {
            if (view == null) {
                view = LayoutInflater.from(context).inflate(R.layout.view_pager_item, viewGroup, false);
            }
            ViewPagerBean viewPagerBean = (ViewPagerBean) list.get(position);
            ImageView img_user = view.findViewById(R.id.img_user);
            TextView t_sub_head = view.findViewById(R.id.t_sub_head);
            TextView t_user_name = view.findViewById(R.id.t_user_name);
            TextView t_intro = view.findViewById(R.id.t_intro);
            TextView t_show_time = view.findViewById(R.id.t_show_time);
            TextView t_show_address = view.findViewById(R.id.t_show_address);
            TextView t_show_phone = view.findViewById(R.id.t_show_phone);
            t_sub_head.setText(viewPagerBean.getT_sub_head());
            t_user_name.setText(viewPagerBean.getT_user_name());
            t_intro.setText(viewPagerBean.getT_intro());
            t_show_time.setText(viewPagerBean.getT_show_time());
            t_show_address.setText(viewPagerBean.getT_show_address());
            t_show_phone.setText(viewPagerBean.getT_show_phone());
            return view;
        }
    }
}
